﻿#region [ License information          ]
/* ************************************************************
 *
 * Copyright (c) Bjørn Bouet Smith, 2012
 *
 * This source code is subject to terms and conditions of 
 * Microsoft Public License (Ms-PL).
 * 
 * A copy of the license can be found in the license.txt
 * file at the root of this distribution. If you can not 
 * locate the License, please send an email to bjornsmith@gmail.com
 * 
 * By using this source code in any fashion, you are 
 * agreeing to be bound by the terms of the Microsoft 
 * Public License.
 *
 * You must not remove this notice, or any other, from this
 * software.
 *
 * ************************************************************/
#endregion

using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Smith.BuildExtensions.ConfigurationTransformation;
using Smith.BuildExtensions.Logging;

namespace Smith.BuildExtensions.BuildTasks
{
    /// <summary>
    /// <UsingTask TaskName="ConfigTransformTask" AssemblyFile="Smith.BuildExtensions.dll" />
    /// <Target Name="BeforeBuild">
    /// <ConfigTransformTask ConfigTemplate="App.config.base.config" Configuration="$(Configuration)" Outputfile=".\App.config" />
    /// </Target>
    /// </summary>
    public class ConfigTransformTask : Task
    {
        /// <summary>
        /// When overridden in a derived class, executes the task.
        /// </summary>
        /// <returns>
        /// true if the task successfully executed; otherwise, false.
        /// </returns>
        public override bool Execute()
        {
            LogWriter writer = new LogWriter(Log);

            try
            {
                writer.LogMessage(
                   new Message(
                       MessageLevel.Info,
                       "Doing configuration tranformation in directory:{0} for configuration:{1} in project file",
                       ConfigBaseDir,
                       Configuration));


                string projectFileName = BuildEngine.ProjectFileOfTaskNode;
                ConfigReader configReader = new ConfigReader(ConfigBaseDir ?? Environment.CurrentDirectory, ConfigTemplate, Configuration, projectFileName);

                ConfigWriter configWriter = new ConfigWriter(OutputFile);

                ValueSelector valueSelector = new ValueSelector(writer, configReader);

                ConfigTransformer transformer = new ConfigTransformer(configReader, new KeyReader(), valueSelector);

                ConfigTransformationEngine engine = new ConfigTransformationEngine(transformer, configWriter);
                engine.DoTransformation();
            }
            catch (Exception e)
            {
                writer.LogMessage(new Message(MessageLevel.Error, e.ToString()));
                return false;
            }

            return true;
        }

        /// <summary>
        /// Gets or sets the build configuration.
        /// </summary>
        [Required]
        public string Configuration
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the output filename.
        /// </summary>
        [Required]
        public string OutputFile
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the config template file name.
        /// </summary>
        [Required]
        public string ConfigTemplate
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the config base dir, use an empty string for Environment.CurrentDirectory
        /// </summary>
        public string ConfigBaseDir
        {
            get;
            set;
        }
    }
}
